﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using ShowroomMobile.Controls;

namespace ShowroomMobile
{
    public partial class AddProduct : AdminBasePage
    {
        private int CategoryID
        {
            get
            {
                return string.IsNullOrEmpty(Request["cid"]) ? 0 : Convert.ToInt32(Request["cid"]);
            }
        }

        private string _textSearch;
        private string TextSearch
        {
            get
            {
                return _textSearch;
            }
            set
            {
                _textSearch = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gridProducts.Rebind();
            }
        }

        protected void gridProducts_OnNeedDataSource(object sender, EventArgs e)
        {
            View_Products prods = new View_Products();
            string sFilter = string.Format(View_Products.ColumnNames.ID + " NOT IN ({0})", CategoryProduct.GetListCurrentProductID(CategoryID));
            if (!string.IsNullOrEmpty(TextSearch))
            {
                sFilter += string.Format(" AND {0} LIKE '%{1}%'", View_Products.ColumnNames.Name, TextSearch);
            }
            int total = prods.Search(sFilter, "[ID] desc", gridProducts.CurrentPageIndex * gridProducts.PageSize, gridProducts.PageSize);
            gridProducts.VirtualItemCount = total;
            gridProducts.DataSource = prods.DefaultView;
        }

        protected void btnAddSelectedProduct_Click(object sender, EventArgs e)
        {
            try
            {
                string sListID = hidSelectedProductID.Value;
                if (!string.IsNullOrEmpty(sListID))
                {
                    string[] IDs = sListID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    CategoryProduct cProd = new CategoryProduct();

                    foreach (string id in IDs)
                    {
                        cProd.AddNew();
                        cProd.s_ProductID = id;
                        cProd.CategoryID = CategoryID;
                    }
                    cProd.Save();
                    gridProducts.Rebind();
                    hidSelectedProductID.Value = string.Empty;
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("AddProduct.cs", "btnAddSelectedProduct_Click", ex.StackTrace);
            }
        }

        protected void gridProducts_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            gridProducts.CurrentPageIndex = e.NewPageIndex;
            gridProducts.Rebind();
        }

        protected void btnSearchProduct_Click(object sender, EventArgs e)
        {
            TextSearch = txtSearchProduct.Text.Trim();
            gridProducts.CurrentPageIndex = 0;
            gridProducts.Rebind();
        }
    }
}